package com.atguigu.gmall.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import sun.management.ThreadInfoCompositeData;

import javax.annotation.PreDestroy;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author :ttm
 * @description :
 * @create :2021-12-25 18:29:00
 */
@RestController
@RequestMapping("/api/user/passport")
public class PassportApiController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("login")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest request){
        UserInfo info = userService.login(userInfo);
        if (info!=null){
            //生成token，并放入cookie中，还存储一个userinfo里面的数据是map！
            HashMap<String, Object> map = new HashMap<>();
            String token = UUID.randomUUID().toString();

            map.put("token",token);
            //需要存储一个用户昵称
            map.put("nickName",info.getNickName());
            //将用户信息放入缓存放入缓存
            String userKey= RedisConst.USER_LOGIN_KEY_PREFIX+token;
            String ipAddress = IpUtil.getIpAddress(request);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId",info.getId().toString());
            jsonObject.put("ip",ipAddress);
            //将数据存储到缓存
            this.redisTemplate.opsForValue().set(userKey,jsonObject.toJSONString(),RedisConst.USERKEY_TIMEOUT, TimeUnit.SECONDS);
            return Result.ok(map);
        }else {
            return Result.fail().message("登录失败");
        }
    }
    /**
     * 退出登录
     * @param request
     * @return
     */
    @GetMapping("logout")
    public Result logout(@RequestHeader String token, HttpServletRequest request){
        //要删除token和userinfo
        //如何获取token
//        String token="";
//        Cookie[] cookies = request.getCookies();
//        if (cookies!=null &&cookies.length>0){
//            for (Cookie cookie : cookies) {
//                if (("token").equals(cookie.getName())){
//                     token = cookie.getValue();
//                }
//            }
//        }

        String userKey= RedisConst.USER_LOGIN_KEY_PREFIX+token;
//        Cookie[] cookies = request.getCookies();
//        if (cookies!=null){
//            for (Cookie cookie : cookies) {
//                if ("token".equals(cookie.getName())){
//                    token=cookie.getValue();
//                }
//            }
//        }
        this.redisTemplate.delete(userKey);
        return Result.ok();

    }
}
